package cn.com.egova.erp.action;

import cn.com.egova.erp.biz.ISupplierBiz;
import cn.com.egova.erp.entity.Supplier;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 供应商Action
 *
 * @author Administrator
 */
public class SupplierAction extends BaseAction<Supplier> {

    private ISupplierBiz supplierBiz;

    private File file;                //上传文件
    private String fileFileName;      //上传文件名称
    private String fileContentType;   //上传文件类型

    public void setSupplierBiz(ISupplierBiz supplierBiz) {
        this.supplierBiz = supplierBiz;
        super.setBaseBiz(this.supplierBiz);
    }

    private String q;

    public String getQ() {
        return q;
    }

    public void setQ(String q) {
        this.q = q;
    }


    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }


    public void list() {
        //构建查询条件
        Supplier t1 = getT1();
        if (t1 == null) {
            setT1(new Supplier());
        }
        getT1().setName(q);
        super.list();
    }


    /**
     * 导出供应商/客户
     */
    public void export() {
        String fileName = "";
        if (Supplier.TYPE_SUPPLIER.equals(getT1().getType())) {
            fileName = "供应商.xls";
        } else {
            fileName = "客户.xls";
        }
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            //设置输出流，实现下载，指定是excel,因为文件名有中文，故还要解决中文乱码
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
            supplierBiz.export(response.getOutputStream(), getT1());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 导入数据
     */
    public void doImport() {
//        if (!"application/vnd.ms-excel".equals(fileContentType)) {
//            ajaxReturn(false, "上传文件必须为excel文件!");
//            return;
//        }
        try {
            //调用导入业务
            supplierBiz.doImport(new FileInputStream(file));
            ajaxReturn(true, "导入成功!");
        } catch (IOException e) {
            e.printStackTrace();
            ajaxReturn(true, "导入失败!");
        }
    }
}
